#include "StorageManager.h"
#include "FileManager.h"
#include "BufferManager.h"
#include <iostream>

int main()
{
    std::cout << "Testing components..." << std::endl;

    try {
        std::cout << "1. Testing FileManager..." << std::endl;
        auto& fm = MiniDB::FileManager::getInstance();
        std::cout << "FileManager instance obtained" << std::endl;

        std::cout << "2. Testing BufferManager..." << std::endl;
        MiniDB::BufferManager bm(20, MiniDB::ReplacementPolicy::LRU);
        std::cout << "BufferManager created successfully" << std::endl;

        std::cout << "3. Testing StorageManager..." << std::endl;
        MiniDB::StorageManager sm(20, MiniDB::ReplacementPolicy::LRU);
        std::cout << "StorageManager created successfully" << std::endl;

        std::cout << "4. Testing table creation..." << std::endl;
        if (sm.createTable("test_table")) {
            std::cout << "Table created successfully" << std::endl;
        } else {
            std::cout << "Table creation failed" << std::endl;
            return 1;
        }

        std::cout << "5. Testing record insertion..." << std::endl;
        MiniDB::Record record(1, "Test data");
        if (sm.insertRecord("test_table", record)) {
            std::cout << "Record inserted successfully" << std::endl;
        } else {
            std::cout << "Record insertion failed" << std::endl;
            return 1;
        }

        std::cout << "6. Testing record retrieval..." << std::endl;
        auto records = sm.getRecords("test_table");
        std::cout << "Found " << records.size() << " records" << std::endl;

        for (const auto& rec : records) {
            if (!rec.is_deleted) {
                std::cout << "Record: ID=" << rec.id << ", Data='" << rec.data << "'" << std::endl;
            }
        }

    } catch (const std::exception& e) {
        std::cerr << "Exception caught: " << e.what() << std::endl;
        return 1;
    } catch (...) {
        std::cerr << "Unknown exception caught" << std::endl;
        return 1;
    }

    std::cout << "All tests completed successfully" << std::endl;
    return 0;
}
